import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kepu/page/category_result_page/provider/plant_list_by_type_provider.dart';
import 'package:kepu/page/plant_detail_page/plant_detail_page.dart';
import 'package:kepu/widgets/error_widget.dart';
import 'package:kepu/widgets/image_with_title.dart';
import 'package:kepu/widgets/loading_widget.dart';

class PlantCategoryResultPage extends ConsumerWidget {
  const PlantCategoryResultPage({
    super.key,
    required this.id,
    required this.categoryName,
  });

  final String categoryName;
  final int id;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    var result = ref.watch(PlantListByTypeProvider(id: id));
    return Scaffold(
      appBar: AppBar(title: Text("关于$categoryName的分类结果")),
      body: result.when(
        data: (value) {
          if(value.code == 500){
            return Center(child: Text("没有查询到相关信息"),);
          }else{
          return Padding(
            padding: const EdgeInsets.all(15.0),
            child: ListView.separated(
              itemCount: value.data.length,
              itemBuilder: (context, index) {
                return GestureDetector(
                  onTap: () {
                    Navigator.of(context).push(MaterialPageRoute(builder: (_)=>PlantDetailPage(value.data[index].id)));
                  },
                  child: ImageWithBriefInfo(
                    imageUrl: value.data[index].image,
                    title: value.data[index].name,
                    subtitle: value.data[index].description,
                  ),
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return Divider();
              },
            ),
          );
        }},
        error: (e, st) => errorWidget(),
        loading: () => loadWidget(context),
      ),
    );
  }
}
